import { Run as swagger } from "../src/Swagger/js/Utils.js";
import F2S from "../src/Swagger/js/F2S.js";
import chalk from "chalk";
import fs from "fs";
import path from "path";

const TestJson = fs.readFileSync(path.resolve("./Test/swagger.json"), "utf-8");
const TestData = JSON.parse(TestJson);

const Config = {
    // 使用新版 EventName 格式化方法
    NewNameRule: true,
    // 用于去掉重复的 Path 字段
    IgnorePath: ["api", "dsms"],
    // swagger 文档地址
    path: ["http://192.168.11.51:30099/swagger/dsms/index.html"],
    // 接口文件输出地址
    apiPath: "api",
    // 文档输出附加模块(头部模块)
    formatModuleString: `// 经过封装的请求方法地址
    import request from '@/libs/request.js';`,
};

/**
 * 输出文件
 *
 * @param {String} filePath
 * @param {String} content
 */
const WriteFile = (filePath, content) => {
    fs.writeFile(path.resolve(filePath), content, "utf8", function (err) {
        if (err) {
            console.log(chalk.red(err));
        } else {
            console.log(chalk.greenBright(filePath + " : 输出成功!"));
        }
    });
};

/**
 * 导出文件
 *
 * @param {*} options
 * @param {*} config
 * @param {*} TypeStr
 * @param {*} ignore
 */
const ExportsFile = async (File, FileName, config, OutPut) => {
    WriteFile(OutPut + "/" + FileName + ".js", F2S(File, config, FileName));
};

ExportsFile(TestData, "Meteorolog", Config, "api");
